package com.pipe.protocol.events.table.reader;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Kiven
 */

public enum ColumnType {
    /***/
    MYSQL_TYPE_DECIMAL(0x00, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_TINY(0x01, 0, new MysqlTypeTinyReader()),
    /***/
    MYSQL_TYPE_SHORT(0x02, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_LONG(0x03, 4, new MysqlTypeLongReader()),
    /***/
    MYSQL_TYPE_FLOAT(0x04, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_DOUBLE(0x05, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_NULL(0x06, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_TIMESTAMP(0x07, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_LONGLONG(0x08, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_INT24(0x09, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_DATE(0x0a, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_TIME(0x0b, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_DATETIME(0x0c, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_YEAR(0x0d, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_NEWDATE(0x0e, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_VARCHAR(0x0f, 0, new MysqlTypeVarcharReader()),
    /***/
    MYSQL_TYPE_BIT(0x10, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_TIMESTAMP2(0x11, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_DATETIME2(0x12, 0, new MysqlTypeDatetime2Reader()),
    /***/
    MYSQL_TYPE_TIME2(0x13, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_NEWDECIMAL(0xf6, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_ENUM(0xf7, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_SET(0xf8, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_TINY_BLOB(0xf9, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_MEDIUM_BLOB(0xfa, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_LONG_BLOB(0xfb, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_BLOB(0xfc, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_VAR_STRING(0xfd, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_STRING(0xfe, 0, new DefaultReader()),
    /***/
    MYSQL_TYPE_GEOMETRY(0xff, 0, new DefaultReader());

    private int hexValue;
    private int typeLength;
    private ColumnReader columnReader;
    private static Map<Integer, ColumnType> TYPE_MAP = null;

    ColumnType(int hexValue, int typeLength, ColumnReader columnReader) {
        this.hexValue = hexValue;
        this.typeLength = typeLength;
        this.columnReader = columnReader;
    }

    public static ColumnType getColumnType(int hexValue) {
        if (TYPE_MAP == null) {
            initTypeSet();
        }
        return TYPE_MAP.get(hexValue);
    }

    private synchronized static void initTypeSet(){
        TYPE_MAP = new HashMap<>(32);
        TYPE_MAP.put(MYSQL_TYPE_DECIMAL.hexValue, MYSQL_TYPE_DECIMAL);
        TYPE_MAP.put(MYSQL_TYPE_TINY.hexValue, MYSQL_TYPE_TINY);
        TYPE_MAP.put(MYSQL_TYPE_SHORT.hexValue, MYSQL_TYPE_SHORT);
        TYPE_MAP.put(MYSQL_TYPE_LONG.hexValue, MYSQL_TYPE_LONG);
        TYPE_MAP.put(MYSQL_TYPE_FLOAT.hexValue, MYSQL_TYPE_FLOAT);
        TYPE_MAP.put(MYSQL_TYPE_DOUBLE.hexValue, MYSQL_TYPE_DOUBLE);
        TYPE_MAP.put(MYSQL_TYPE_NULL.hexValue, MYSQL_TYPE_NULL);
        TYPE_MAP.put(MYSQL_TYPE_TIMESTAMP.hexValue, MYSQL_TYPE_TIMESTAMP);
        TYPE_MAP.put(MYSQL_TYPE_LONGLONG.hexValue, MYSQL_TYPE_LONGLONG);
        TYPE_MAP.put(MYSQL_TYPE_INT24.hexValue, MYSQL_TYPE_INT24);
        TYPE_MAP.put(MYSQL_TYPE_DATE.hexValue, MYSQL_TYPE_DATE);
        TYPE_MAP.put(MYSQL_TYPE_TIME.hexValue, MYSQL_TYPE_TIME);
        TYPE_MAP.put(MYSQL_TYPE_DATETIME.hexValue, MYSQL_TYPE_DATETIME);
        TYPE_MAP.put(MYSQL_TYPE_YEAR.hexValue, MYSQL_TYPE_YEAR);
        TYPE_MAP.put(MYSQL_TYPE_NEWDATE.hexValue, MYSQL_TYPE_NEWDATE);
        TYPE_MAP.put(MYSQL_TYPE_VARCHAR.hexValue, MYSQL_TYPE_VARCHAR);
        TYPE_MAP.put(MYSQL_TYPE_BIT.hexValue, MYSQL_TYPE_BIT);
        TYPE_MAP.put(MYSQL_TYPE_TIMESTAMP2.hexValue, MYSQL_TYPE_TIMESTAMP2);
        TYPE_MAP.put(MYSQL_TYPE_DATETIME2.hexValue, MYSQL_TYPE_DATETIME2);
        TYPE_MAP.put(MYSQL_TYPE_TIME2.hexValue, MYSQL_TYPE_TIME2);
        TYPE_MAP.put(MYSQL_TYPE_NEWDECIMAL.hexValue, MYSQL_TYPE_NEWDECIMAL);
        TYPE_MAP.put(MYSQL_TYPE_ENUM.hexValue, MYSQL_TYPE_ENUM);
        TYPE_MAP.put(MYSQL_TYPE_SET.hexValue, MYSQL_TYPE_SET);
        TYPE_MAP.put(MYSQL_TYPE_TINY_BLOB.hexValue, MYSQL_TYPE_TINY_BLOB);
        TYPE_MAP.put(MYSQL_TYPE_MEDIUM_BLOB.hexValue, MYSQL_TYPE_MEDIUM_BLOB);
        TYPE_MAP.put(MYSQL_TYPE_LONG_BLOB.hexValue, MYSQL_TYPE_LONG_BLOB);
        TYPE_MAP.put(MYSQL_TYPE_BLOB.hexValue, MYSQL_TYPE_BLOB);
        TYPE_MAP.put(MYSQL_TYPE_VAR_STRING.hexValue, MYSQL_TYPE_VAR_STRING);
        TYPE_MAP.put(MYSQL_TYPE_STRING.hexValue, MYSQL_TYPE_STRING);
        TYPE_MAP.put(MYSQL_TYPE_GEOMETRY.hexValue, MYSQL_TYPE_GEOMETRY);
    }

    public ColumnReader getColumnReader() {
        return columnReader;
    }

    public int getHexValue() {
        return hexValue;
    }
}